package com.sjr.easy.chat.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sjr.easy.chat.model.ChatGroupUser;
import com.sjr.easy.chat.service.ChatGroupUserService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import com.sjr.easy.chat.repo.ChatGroupUserRepo;

import java.util.List;
import java.util.Objects;

@Slf4j
@Service
@Transactional
public class ChatGroupUserServiceImpl implements ChatGroupUserService {

    @Autowired
    private ChatGroupUserRepo chatGroupUserRepo;

    @Override
    public void batchAddChatGroupUser(List<ChatGroupUser> chatGroupUserList) {
        log.info("chatGroupUserList:{}", JSON.toJSONString(chatGroupUserList));
        chatGroupUserRepo.saveBatch(chatGroupUserList, 100);
    }

    @Override
    public List<ChatGroupUser> selectByChatGroupId(Long chatGroupId) {
        log.info("chatGroupId:{}", chatGroupId);
        ChatGroupUser params = new ChatGroupUser();
        params.setChatGroupId(chatGroupId);
        QueryWrapper<ChatGroupUser> queryWrapper = new QueryWrapper<>(params);
        return chatGroupUserRepo.list(queryWrapper);
    }

    @Override
    public List<ChatGroupUser> selectByChatGroupIdList(List<Long> chatGroupIdList) {
        log.info("chatGroupIdList:{}", JSON.toJSONString(chatGroupIdList));
        LambdaQueryWrapper<ChatGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ChatGroupUser::getChatGroupId, chatGroupIdList);
        return chatGroupUserRepo.list(queryWrapper);
    }

    @Override
    public ChatGroupUser selectByUserIdAndChatGroupId(Long userId, Long chatGroupId) {
        log.info("userId:{},chatGroupId:{}", userId, chatGroupId);
        ChatGroupUser params = new ChatGroupUser();
        params.setUserId(userId);
        params.setChatGroupId(chatGroupId);
        QueryWrapper<ChatGroupUser> queryWrapper = new QueryWrapper<>(params);
        return chatGroupUserRepo.getOne(queryWrapper);
    }

    @Override
    public List<ChatGroupUser> selectByUserId(Long userId) {
        log.info("userId:{}", userId);
        ChatGroupUser params = new ChatGroupUser();
        params.setUserId(userId);
        QueryWrapper<ChatGroupUser> queryWrapper = new QueryWrapper<>(params);
        return chatGroupUserRepo.list(queryWrapper);
    }

    @Override
    public void quitChatGroup(Long chatGroupId, Long userId) {
        log.info("chatGroupId:{},userId:{}", chatGroupId, userId);
        Objects.requireNonNull(chatGroupId, "chatGroupId不允许为空");
        Objects.requireNonNull(userId, "userId不允许为空");
        LambdaQueryWrapper<ChatGroupUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ChatGroupUser::getChatGroupId, chatGroupId);
        queryWrapper.eq(ChatGroupUser::getUserId, userId);
        chatGroupUserRepo.remove(queryWrapper);
    }
}
